2020-2021 Sunseeker Telemetry and Lighting System
eusci_a_spi_ex1_3WireMaster.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx EUSCI_A SPI - 3 Wire Incrementing Data Master
34  *
35  * Description: In this example, the device is configured as a SPI master using
36  * ACLK as the clock source. The SPI communication is setup to run at 16kHz.
37  * The initial data is written to the TX Buffer, once the data is sent out the
38  * device gets an RX interrupt that contains the data it recieved from the
39  * slave. The slave and master data values should stay in sync and if there is
40  * a problem in communication the LED will turn on. The device stays in LPM0
41  * and sends the data in the ISR. To properly demo this example connect to
42  * slave version of this example and start the slave device first.
43  *
44  * MSP430i2041
45  * ------------------
46  * /|\| |
47  * | | P1.4|--->LED
48  * --|RST |
49  * | P1.3/UCA0SIMO|--->Data Out
50  * | |
51  * | P1.2/UCA0MISO|<---Data In
52  * | |
53  * | P1.1/UCA0CLK|--->Serial Clock
54  *
55  * Author: Zack Lalanne
56  ******************************************************************************/
57 #include "driverlib.h"
58 
59 uint8_t TXData = 0;
60 uint8_t RXData = 0;
61 
62 int main(void)
63 {
64 
65  EUSCI_A_SPI_initMasterParam spiMasterConfig = {
66  EUSCI_A_SPI_CLOCKSOURCE_ACLK, // ACLK Clock Source
67  32000, // ACLK = 32kHz
68  16000, // SPICLK = 16kHz
69  EUSCI_A_SPI_MSB_FIRST, // MSB First
70  EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT, // Phase
71  EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // High polarity
72  EUSCI_A_SPI_3PIN // 3Wire SPI Mode
73  };
74 
75  WDT_hold(WDT_BASE);
76 
77  // Setting P1.1, P1.2 and P1.3 as SPI pins.
78  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
79  GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3,
80  GPIO_PRIMARY_MODULE_FUNCTION);
81 
82  // Setting P1.4 as LED Pin
83  GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
84  GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
85 
86  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
87  // ACLK is at 32kHz
88  CS_setupDCO(CS_INTERNAL_RESISTOR);
89 
90  // Configure and enable the SPI peripheral
91  EUSCI_A_SPI_initMaster(EUSCI_A0_BASE, &spiMasterConfig);
92  EUSCI_A_SPI_enable(EUSCI_A0_BASE);
93 
94  // Put the first byte in the transfer buffer
95  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, TXData);
96 
97  EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
98 
99  // Go into LPM0 with interrupts enabled
100  __bis_SR_register(LPM0_bits | GIE);
101 }
102 
103 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
104 #pragma vector=USCI_A0_VECTOR
105 __interrupt
106 #elif defined(__GNUC__)
107 __attribute__((interrupt(USCI_A0_VECTOR)))
108 #endif
109 void USCI_A0_ISR(void)
110 {
111  switch(__even_in_range(UCA0IV, USCI_SPI_UCTXIFG)) {
112  case USCI_NONE: break;
113  case USCI_SPI_UCRXIFG:
114  // Read what slave sent. Should be same as transferred. If there
115  // is a mismatch turn on the LED
116  RXData = EUSCI_A_SPI_receiveData(EUSCI_A0_BASE);
117  if(RXData != TXData)
118  {
119  GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
120  while(1);
121  }
122  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, ++TXData);
123  break;
124  case USCI_SPI_UCTXIFG:
125  break;
126  default: break;
127  }
128 }
129 
uint8_t RXData
void USCI_A0_ISR(void)
int main(void)
uint8_t TXData
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)